import { Injectable } from '@angular/core';
import {
  Router,
  CanActivate,
  NavigationEnd,
  NavigationError,
  NavigationCancel } from '@angular/router';
import {environment} from '@env/environment';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class AuthService implements CanActivate {

  constructor(public router: Router, private http: HttpClient) {}

  // 获取token，并将token存储在localstorage中
  getToken(params) {
    return new Promise((resolve, reject) => {
      this.http.post(`${environment.GATEWAYURL}api/authenticate`, params).subscribe(res => {
        resolve(res);
      }, error => {
        reject(error);
      });
    });
  }

  canActivate(): boolean {
    this.router.events.subscribe(evt => {
      if (evt instanceof NavigationEnd) {
        console.log(this.router);
      }
      if (evt instanceof NavigationError && evt instanceof NavigationCancel) {

      }
    });
    return true;
  }
}
